package org.huzhp.data.core;

import cn.hutool.core.lang.Assert;

/**
 * @Description: DbInfoBuilder
 * @Author: huzp
 * @CreateDate: 2020/4/22 11:55
 * @Version: 1.0
 */
public class DbInfoBuilder {

    private String id;

    private String type;

    private String ip;

    private Integer port;

    private String schema;

    private String name;

    private String username;

    private String password;

    private String jdbcUrl;

    public DbInfoBuilder id(String id){
        this.id = id;
        return this;
    }

    public DbInfoBuilder type(String type){
        this.type = type;
        if (this.port==null){
            this.port = DbType.getTypes().get(type.toUpperCase()).getPort();
        }
        return this;
    }

    public DbInfoBuilder ip(String ip){
        this.ip = ip;
        return this;
    }

    public DbInfoBuilder port(Integer port){
        this.port = port;
        return this;
    }


    public DbInfoBuilder schema(String schema){
        this.schema = schema;
        return this;
    }

    public DbInfoBuilder name(String name){
        this.name = name;
        return this;
    }

    public DbInfoBuilder username(String username){
        this.username = username;
        return this;
    }

    public DbInfoBuilder password(String password){
        this.password = password;
        return this;
    }

    public DbInfoBuilder jdbcUrl(String jdbcUrl){
        this.jdbcUrl = jdbcUrl;
        return this;
    }

    public String getId() {
        return id;
    }

    public String getType() {
        return type;
    }

    public String getIp() {
        return ip;
    }

    public Integer getPort() {
        return port;
    }

    public String getSchema() {
        return schema;
    }

    public String getName() {
        return name;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getJdbcUrl() {
        return jdbcUrl;
    }

    public DbInfo build(){
        if (this.jdbcUrl==null){
            Assert.isFalse(this.ip == null || this.port == null || this.name == null,"ip/port/name must not be null");
            this.jdbcUrl = DbType.getTypes().get(type.toUpperCase()).getUrl().replace("{IP}",this
            .ip).replace("{PORT}",this.port.toString()).replace("{NAME}",this.name);
        }
        return new DbInfo(this);
    }




}
